package com.chen.spider.controller;

import com.alibaba.fastjson.JSONObject;
import com.chen.spider.config.MyPipeline;
import com.chen.spider.config.MyProcessor;
import com.chen.spider.service.SpiderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.scheduler.QueueScheduler;

import javax.annotation.Resource;


//@Slf4j
@Api(tags = "爬虫")
@RestController
public class SpiderController {

    @Resource
    private MyPipeline myPipeline;
    @Resource
    private MyProcessor myProcessor;
    @Resource
    private SpiderService spiderService;

    @PostMapping("/start")
    public void start() {
        Spider.create(myProcessor)  //new MyProcessor()
                .addUrl("https://www.pkufh.com/Html/News/Columns/1064/1.html")
//                .addPipeline(new ConsolePipeline())
                .addPipeline(myPipeline)
                .setScheduler(new QueueScheduler())  //添加队列防止抓取重复
                .run();
    }
    @ApiOperation(value="计算爬取数据的数量")
    @PostMapping("/count")
    public Object count(){
        JSONObject jsonObject = new JSONObject();
        if(!spiderService.isCount()){
            jsonObject.put("code",0);
            jsonObject.put("message","未知异常");
            return jsonObject;
        }
        jsonObject.put("code",1);
        jsonObject.put("message","当前数据有"+spiderService.count()+"条");
        return jsonObject;

    }


}
